<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Linux Terminal Simulator - HTML5</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            background: linear-gradient(135deg, #2c3e50, #34495e);
            font-family: 'Courier New', monospace;
            height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
        }

        .terminal-container {
            width: 90%;
            max-width: 1000px;
            height: 600px;
            background: #1e1e1e;
            border-radius: 10px;
            box-shadow: 0 20px 40px rgba(0,0,0,0.3);
            overflow: hidden;
            border: 1px solid #333;
        }

        .terminal-header {
            background: linear-gradient(90deg, #ff5f56, #ffbd2e, #27ca3f);
            height: 30px;
            display: flex;
            align-items: center;
            padding: 0 15px;
            position: relative;
        }

        .terminal-buttons {
            display: flex;
            gap: 8px;
        }

        .terminal-button {
            width: 12px;
            height: 12px;
            border-radius: 50%;
            cursor: pointer;
        }

        .close { background: #ff5f56; }
        .minimize { background: #ffbd2e; }
        .maximize { background: #27ca3f; }

        .terminal-title {
            position: absolute;
            left: 50%;
            transform: translateX(-50%);
            color: #000;
            font-size: 12px;
            font-weight: bold;
        }

        .terminal-body {
            height: calc(100% - 30px);
            background: #1e1e1e;
            color: #00ff00;
            padding: 20px;
            overflow-y: auto;
            font-size: 14px;
            line-height: 1.4;
        }

        .terminal-output {
            white-space: pre-wrap;
            word-wrap: break-word;
        }

        .terminal-input-line {
            display: flex;
            align-items: center;
            margin-top: 10px;
        }

        .prompt {
            color: #00ff00;
            margin-right: 5px;
        }

        .terminal-input {
            background: transparent;
            border: none;
            color: #00ff00;
            font-family: inherit;
            font-size: inherit;
            outline: none;
            flex: 1;
        }

        .cursor {
            background: #00ff00;
            animation: blink 1s infinite;
        }

        @keyframes blink {
            0%, 50% { opacity: 1; }
            51%, 100% { opacity: 0; }
        }

        .file-link {
            color: #3498db;
            text-decoration: none;
        }

        .file-link:hover {
            text-decoration: underline;
        }

        .error {
            color: #e74c3c;
        }

        .success {
            color: #2ecc71;
        }

        .warning {
            color: #f39c12;
        }

        .directory {
            color: #3498db;
            font-weight: bold;
        }

        .executable {
            color: #2ecc71;
            font-weight: bold;
        }

        .process-table {
            border-collapse: collapse;
            width: 100%;
            margin: 10px 0;
        }

        .process-table th,
        .process-table td {
            border: 1px solid #444;
            padding: 5px;
            text-align: left;
        }

        .process-table th {
            background: #333;
        }
    </style>
</head>
<body>
    <div class="terminal-container">
        <div class="terminal-header">
            <div class="terminal-buttons">
                <div class="terminal-button close"></div>
                <div class="terminal-button minimize"></div>
                <div class="terminal-button maximize"></div>
            </div>
            <div class="terminal-title">Terminal - Linux Simulator</div>
        </div>
        <div class="terminal-body">
            <div class="terminal-output" id="output">
Welcome to Linux Terminal Simulator (HTML5)
Type 'help' for available commands

</div>
            <div class="terminal-input-line">
                <span class="prompt" id="prompt">user@linux:~$</span>
                <input type="text" class="terminal-input" id="input" autocomplete="off">
                <span class="cursor">_</span>
            </div>
        </div>
    </div>

    <script>
        class LinuxSimulator {
            constructor() {
                this.currentPath = '/home/user';
                this.fileSystem = {
                    '/': {
                        type: 'directory',
                        children: {
                            'home': {
                                type: 'directory',
                                children: {
                                    'user': {
                                        type: 'directory',
                                        children: {
                                            'documents': { type: 'directory', children: {} },
                                            'downloads': { type: 'directory', children: {} },
                                            'desktop': { type: 'directory', children: {} },
                                            'test.txt': { type: 'file', content: 'Hello World!\nThis is a test file.' },
                                            'script.sh': { type: 'file', content: '#!/bin/bash\necho "Hello from script"', executable: true }
                                        }
                                    }
                                }
                            },
                            'etc': {
                                type: 'directory',
                                children: {
                                    'passwd': { type: 'file', content: 'root:x:0:0:root:/root:/bin/bash\nuser:x:1000:1000:user:/home/user:/bin/bash' },
                                    'hosts': { type: 'file', content: '127.0.0.1 localhost\n::1 localhost' }
                                }
                            },
                            'var': { type: 'directory', children: {} },
                            'tmp': { type: 'directory', children: {} }
                        }
                    }
                };
                this.processes = [
                    { pid: 1, user: 'root', cpu: 0.1, mem: 0.2, command: 'systemd' },
                    { pid: 123, user: 'user', cpu: 2.3, mem: 1.5, command: 'bash' },
                    { pid: 456, user: 'user', cpu: 0.0, mem: 0.8, command: 'vim' },
                    { pid: 789, user: 'root', cpu: 0.5, mem: 0.3, command: 'nginx' }
                ];
                this.commandHistory = [];
                this.historyIndex = -1;
                this.setupEventListeners();
            }

            setupEventListeners() {
                const input = document.getElementById('input');
                input.addEventListener('keydown', (e) => {
                    if (e.key === 'Enter') {
                        this.executeCommand(input.value);
                        input.value = '';
                    } else if (e.key === 'ArrowUp') {
                        e.preventDefault();
                        this.navigateHistory(-1);
                    } else if (e.key === 'ArrowDown') {
                        e.preventDefault();
                        this.navigateHistory(1);
                    }
                });
                input.focus();
            }

            navigateHistory(direction) {
                const input = document.getElementById('input');
                if (direction === -1 && this.historyIndex < this.commandHistory.length - 1) {
                    this.historyIndex++;
                    input.value = this.commandHistory[this.commandHistory.length - 1 - this.historyIndex];
                } else if (direction === 1 && this.historyIndex > 0) {
                    this.historyIndex--;
                    input.value = this.commandHistory[this.commandHistory.length - 1 - this.historyIndex];
                } else if (direction === 1 && this.historyIndex === 0) {
                    this.historyIndex = -1;
                    input.value = '';
                }
            }

            executeCommand(command) {
                if (command.trim()) {
                    this.commandHistory.push(command);
                    this.historyIndex = -1;
                }

                this.appendOutput(`<span class="prompt">${document.getElementById('prompt').textContent}</span> ${command}\n`);

                const args = command.trim().split(/\s+/);
                const cmd = args[0];

                switch (cmd) {
                    case 'help':
                        this.showHelp();
                        break;
                    case 'ls':
                        this.listFiles(args);
                        break;
                    case 'pwd':
                        this.printWorkingDirectory();
                        break;
                    case 'cd':
                        this.changeDirectory(args[1]);
                        break;
                    case 'mkdir':
                        this.makeDirectory(args[1]);
                        break;
                    case 'rmdir':
                        this.removeDirectory(args[1]);
                        break;
                    case 'rm':
                        this.removeFile(args);
                        break;
                    case 'touch':
                        this.createFile(args[1]);
                        break;
                    case 'cat':
                        this.catFile(args[1]);
                        break;
                    case 'more':
                    case 'less':
                        this.viewFile(args[1]);
                        break;
                    case 'head':
                        this.headFile(args[1], args.includes('-n') ? parseInt(args[args.indexOf('-n') + 1]) : 10);
                        break;
                    case 'tail':
                        this.tailFile(args[1], args.includes('-n') ? parseInt(args[args.indexOf('-n') + 1]) : 10);
                        break;
                    case 'cp':
                        this.copyFile(args[1], args[2]);
                        break;
                    case 'mv':
                        this.moveFile(args[1], args[2]);
                        break;
                    case 'ps':
                        this.showProcesses(args);
                        break;
                    case 'top':
                    case 'htop':
                        this.showTop();
                        break;
                    case 'df':
                        this.showDiskUsage();
                        break;
                    case 'free':
                        this.showMemoryUsage();
                        break;
                    case 'ping':
                        this.ping(args[1]);
                        break;
                    case 'ifconfig':
                    case 'ip':
                        this.showNetworkInfo();
                        break;
                    case 'wget':
                    case 'curl':
                        this.downloadFile(args[1]);
                        break;
                    case 'chmod':
                        this.changePermissions(args[1], args[2]);
                        break;
                    case 'chown':
                        this.changeOwner(args[1], args[2]);
                        break;
                    case 'passwd':
                        this.changePassword(args[1]);
                        break;
                    case 'tar':
                        this.tarOperation(args);
                        break;
                    case 'zip':
                        this.zipFiles(args);
                        break;
                    case 'unzip':
                        this.unzipFile(args[1]);
                        break;
                    case 'ssh':
                        this.sshConnect(args[1]);
                        break;
                    case 'scp':
                        this.scpTransfer(args[1], args[2]);
                        break;
                    case 'clear':
                        this.clearScreen();
                        break;
                    case 'exit':
                        this.appendOutput('<span class="success">Goodbye!</span>\n');
                        break;
                    case 'whoami':
                        this.appendOutput('user\n');
                        break;
                    case 'date':
                        this.appendOutput(new Date().toString() + '\n');
                        break;
                    case 'uname':
                        this.appendOutput('Linux simulator 5.4.0 #1 SMP x86_64 GNU/Linux\n');
                        break;
                    default:
                        if (command.trim()) {
                            this.appendOutput(`<span class="error">bash: ${cmd}: command not found</span>\n`);
                        }
                }

                this.updatePrompt();
            }

            showHelp() {
                const helpText = `Available commands:
File Operations:
  ls [-la]        - List directory contents
  pwd             - Print working directory
  cd [dir]        - Change directory
  mkdir <dir>     - Create directory
  rmdir <dir>     - Remove directory
  rm [-rf] <file> - Remove files/directories
  touch <file>    - Create empty file
  cat <file>      - Display file content
  more/less <file>- View file with pagination
  head [-n] <file>- Show first lines of file
  tail [-n] <file>- Show last lines of file
  cp <src> <dst>  - Copy files
  mv <src> <dst>  - Move/rename files

System Information:
  ps [aux]        - Show running processes
  top/htop        - Show system processes
  df              - Show disk usage
  free            - Show memory usage
  uname           - Show system information
  whoami          - Show current user
  date            - Show current date/time

Network:
  ping <host>     - Ping a host
  ifconfig/ip     - Show network interfaces
  wget/curl <url> - Download files

Permissions:
  chmod <mode> <file> - Change file permissions
  chown <user> <file> - Change file owner
  passwd [user]       - Change password

Archives:
  tar [options] <file> - Archive operations
  zip <archive> <files> - Create zip archive
  unzip <archive>      - Extract zip archive

Remote:
  ssh <user@host>     - SSH connection
  scp <src> <dst>     - Secure copy

Other:
  clear           - Clear screen
  exit            - Exit terminal
  help            - Show this help

`;
                this.appendOutput(helpText);
            }

            listFiles(args) {
                const showAll = args.includes('-a') || args.includes('-la');
                const longFormat = args.includes('-l') || args.includes('-la');
                
                const currentDir = this.getCurrentDirectory();
                if (!currentDir || currentDir.type !== 'directory') {
                    this.appendOutput('<span class="error">ls: cannot access directory</span>\n');
                    return;
                }

                let files = Object.keys(currentDir.children);
                
                if (showAll) {
                    files = ['.', '..', ...files];
                }

                if (longFormat) {
                    this.appendOutput('total ' + files.length + '\n');
                    files.forEach(name => {
                        if (name === '.' || name === '..') {
                            this.appendOutput(`drwxr-xr-x 2 user user 4096 ${new Date().toLocaleDateString()} <span class="directory">${name}</span>\n`);
                        } else {
                            const item = currentDir.children[name];
                            const permissions = item.type === 'directory' ? 'drwxr-xr-x' : (item.executable ? '-rwxr-xr-x' : '-rw-r--r--');
                            const size = item.type === 'directory' ? '4096' : (item.content ? item.content.length : '0');
                            const className = item.type === 'directory' ? 'directory' : (item.executable ? 'executable' : '');
                            this.appendOutput(`${permissions} 1 user user ${size} ${new Date().toLocaleDateString()} <span class="${className}">${name}</span>\n`);
                        }
                    });
                } else {
                    files.forEach(name => {
                        if (name === '.' || name === '..') {
                            this.appendOutput(`<span class="directory">${name}</span>  `);
                        } else {
                            const item = currentDir.children[name];
                            const className = item.type === 'directory' ? 'directory' : (item.executable ? 'executable' : '');
                            this.appendOutput(`<span class="${className}">${name}</span>  `);
                        }
                    });
                    this.appendOutput('\n');
                }
            }

            printWorkingDirectory() {
                this.appendOutput(this.currentPath + '\n');
            }

            changeDirectory(path) {
                if (!path || path === '~') {
                    this.currentPath = '/home/user';
                    return;
                }

                if (path === '..') {
                    const parts = this.currentPath.split('/').filter(p => p);
                    if (parts.length > 0) {
                        parts.pop();
                        this.currentPath = '/' + parts.join('/');
                        if (this.currentPath === '/') this.currentPath = '/';
                    }
                    return;
                }

                if (path === '/') {
                    this.currentPath = '/';
                    return;
                }

                let newPath;
                if (path.startsWith('/')) {
                    newPath = path;
                } else {
                    newPath = this.currentPath === '/' ? '/' + path : this.currentPath + '/' + path;
                }

                const targetDir = this.getDirectoryByPath(newPath);
                if (targetDir && targetDir.type === 'directory') {
                    this.currentPath = newPath;
                } else {
                    this.appendOutput(`<span class="error">cd: ${path}: No such file or directory</span>\n`);
                }
            }

            makeDirectory(name) {
                if (!name) {
                    this.appendOutput('<span class="error">mkdir: missing operand</span>\n');
                    return;
                }

                const currentDir = this.getCurrentDirectory();
                if (currentDir && currentDir.children) {
                    if (currentDir.children[name]) {
                        this.appendOutput(`<span class="error">mkdir: cannot create directory '${name}': File exists</span>\n`);
                    } else {
                        currentDir.children[name] = { type: 'directory', children: {} };
                        this.appendOutput(`<span class="success">Directory '${name}' created</span>\n`);
                    }
                }
            }

            removeDirectory(name) {
                if (!name) {
                    this.appendOutput('<span class="error">rmdir: missing operand</span>\n');
                    return;
                }

                const currentDir = this.getCurrentDirectory();
                if (currentDir && currentDir.children && currentDir.children[name]) {
                    const target = currentDir.children[name];
                    if (target.type === 'directory') {
                        if (Object.keys(target.children).length === 0) {
                            delete currentDir.children[name];
                            this.appendOutput(`<span class="success">Directory '${name}' removed</span>\n`);
                        } else {
                            this.appendOutput(`<span class="error">rmdir: failed to remove '${name}': Directory not empty</span>\n`);
                        }
                    } else {
                        this.appendOutput(`<span class="error">rmdir: failed to remove '${name}': Not a directory</span>\n`);
                    }
                } else {
                    this.appendOutput(`<span class="error">rmdir: failed to remove '${name}': No such file or directory</span>\n`);
                }
            }

            removeFile(args) {
                if (args.length < 2) {
                    this.appendOutput('<span class="error">rm: missing operand</span>\n');
                    return;
                }

                const recursive = args.includes('-r') || args.includes('-rf');
                const force = args.includes('-f') || args.includes('-rf');
                const filename = args[args.length - 1];

                const currentDir = this.getCurrentDirectory();
                if (currentDir && currentDir.children && currentDir.children[filename]) {
                    const target = currentDir.children[filename];
                    if (target.type === 'directory' && !recursive) {
                        this.appendOutput(`<span class="error">rm: cannot remove '${filename}': Is a directory</span>\n`);
                    } else {
                        delete currentDir.children[filename];
                        this.appendOutput(`<span class="success">Removed '${filename}'</span>\n`);
                    }
                } else {
                    if (!force) {
                        this.appendOutput(`<span class="error">rm: cannot remove '${filename}': No such file or directory</span>\n`);
                    }
                }
            }

            createFile(name) {
                if (!name) {
                    this.appendOutput('<span class="error">touch: missing operand</span>\n');
                    return;
                }

                const currentDir = this.getCurrentDirectory();
                if (currentDir && currentDir.children) {
                    currentDir.children[name] = { type: 'file', content: '' };
                    this.appendOutput(`<span class="success">File '${name}' created</span>\n`);
                }
            }

            catFile(name) {
                if (!name) {
                    this.appendOutput('<span class="error">cat: missing operand</span>\n');
                    return;
                }

                const currentDir = this.getCurrentDirectory();
                if (currentDir && currentDir.children && currentDir.children[name]) {
                    const file = currentDir.children[name];
                    if (file.type === 'file') {
                        this.appendOutput((file.content || '') + '\n');
                    } else {
                        this.appendOutput(`<span class="error">cat: ${name}: Is a directory</span>\n`);
                    }
                } else {
                    this.appendOutput(`<span class="error">cat: ${name}: No such file or directory</span>\n`);
                }
            }

            viewFile(name) {
                this.catFile(name);
            }

            headFile(name, lines = 10) {
                if (!name) {
                    this.appendOutput('<span class="error">head: missing operand</span>\n');
                    return;
                }

                const currentDir = this.getCurrentDirectory();
                if (currentDir && currentDir.children && currentDir.children[name]) {
                    const file = currentDir.children[name];
                    if (file.type === 'file') {
                        const content = file.content || '';
                        const fileLines = content.split('\n');
                        const outputLines = fileLines.slice(0, lines);
                        this.appendOutput(outputLines.join('\n') + '\n');
                    } else {
                        this.appendOutput(`<span class="error">head: ${name}: Is a directory</span>\n`);
                    }
                } else {
                    this.appendOutput(`<span class="error">head: ${name}: No such file or directory</span>\n`);
                }
            }

            tailFile(name, lines = 10) {
                if (!name) {
                    this.appendOutput('<span class="error">tail: missing operand</span>\n');
                    return;
                }

                const currentDir = this.getCurrentDirectory();
                if (currentDir && currentDir.children && currentDir.children[name]) {
                    const file = currentDir.children[name];
                    if (file.type === 'file') {
                        const content = file.content || '';
                        const fileLines = content.split('\n');
                        const outputLines = fileLines.slice(-lines);
                        this.appendOutput(outputLines.join('\n') + '\n');
                    } else {
                        this.appendOutput(`<span class="error">tail: ${name}: Is a directory</span>\n`);
                    }
                } else {
                    this.appendOutput(`<span class="error">tail: ${name}: No such file or directory</span>\n`);
                }
            }

            copyFile(src, dst) {
                if (!src || !dst) {
                    this.appendOutput('<span class="error">cp: missing operand</span>\n');
                    return;
                }

                const currentDir = this.getCurrentDirectory();
                if (currentDir && currentDir.children && currentDir.children[src]) {
                    const sourceFile = currentDir.children[src];
                    if (sourceFile.type === 'file') {
                        currentDir.children[dst] = { ...sourceFile };
                        this.appendOutput(`<span class="success">Copied '${src}' to '${dst}'</span>\n`);
                    } else {
                        this.appendOutput(`<span class="error">cp: omitting directory '${src}'</span>\n`);
                    }
                } else {
                    this.appendOutput(`<span class="error">cp: cannot stat '${src}': No such file or directory</span>\n`);
                }
            }

            moveFile(src, dst) {
                if (!src || !dst) {
                    this.appendOutput('<span class="error">mv: missing operand</span>\n');
                    return;
                }

                const currentDir = this.getCurrentDirectory();
                if (currentDir && currentDir.children && currentDir.children[src]) {
                    const sourceFile = currentDir.children[src];
                    currentDir.children[dst] = sourceFile;
                    delete currentDir.children[src];
                    this.appendOutput(`<span class="success">Moved '${src}' to '${dst}'</span>\n`);
                } else {
                    this.appendOutput(`<span class="error">mv: cannot stat '${src}': No such file or directory</span>\n`);
                }
            }

            showProcesses(args) {
                const showAll = args.includes('aux') || args.includes('-e');
                
                this.appendOutput('<table class="process-table">');
                this.appendOutput('<tr><th>PID</th><th>USER</th><th>%CPU</th><th>%MEM</th><th>COMMAND</th></tr>');
                
                this.processes.forEach(proc => {
                    this.appendOutput(`<tr><td>${proc.pid}</td><td>${proc.user}</td><td>${proc.cpu}</td><td>${proc.mem}</td><td>${proc.command}</td></tr>`);
                });
                
                this.appendOutput('</table>\n');
            }

            showTop() {
                this.appendOutput(`top - ${new Date().toLocaleTimeString()} up 1 day, 2:34, 1 user, load average: 0.15, 0.25, 0.18\n`);
                this.appendOutput('Tasks: 4 total, 1 running, 3 sleeping\n');
                this.appendOutput('%Cpu(s): 2.3 us, 0.7 sy, 0.0 ni, 96.7 id, 0.3 wa\n');
                this.appendOutput('MiB Mem: 8192.0 total, 4096.0 free, 2048.0 used, 2048.0 buff/cache\n\n');
                this.showProcesses(['aux']);
            }

            showDiskUsage() {
                this.appendOutput('Filesystem     1K-blocks    Used Available Use% Mounted on\n');
                this.appendOutput('/dev/sda1       20971520 10485760  10485760  50% /\n');
                this.appendOutput('tmpfs            4194304        0   4194304   0% /tmp\n');
                this.appendOutput('/dev/sda2       10485760  1048576   9437184  10% /home\n');
            }

            showMemoryUsage() {
                this.appendOutput('              total        used        free      shared  buff/cache   available\n');
                this.appendOutput('Mem:        8388608     2097152     4194304      524288     2097152     6291456\n');
                this.appendOutput('Swap:       2097152           0     2097152\n');
            }

            ping(host) {
                if (!host) {
                    this.appendOutput('<span class="error">ping: usage error: Destination address required</span>\n');
                    return;
                }

                this.appendOutput(`PING ${host} (192.168.1.100) 56(84) bytes of data.\n`);
                for (let i = 1; i <= 4; i++) {
                    const time = (Math.random() * 50 + 10).toFixed(1);
                    this.appendOutput(`64 bytes from ${host} (192.168.1.100): icmp_seq=${i} ttl=64 time=${time} ms\n`);
                }
                this.appendOutput(`\n--- ${host} ping statistics ---\n`);
                this.appendOutput('4 packets transmitted, 4 received, 0% packet loss\n');
            }

            showNetworkInfo() {
                this.appendOutput('eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500\n');
                this.appendOutput('        inet 192.168.1.100  netmask 255.255.255.0  broadcast 192.168.1.255\n');
                this.appendOutput('        inet6 fe80::1234:5678:9abc:def0  prefixlen 64  scopeid 0x20<link>\n');
                this.appendOutput('        ether 00:11:22:33:44:55  txqueuelen 1000  (Ethernet)\n\n');
                this.appendOutput('lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536\n');
                this.appendOutput('        inet 127.0.0.1  netmask 255.0.0.0\n');
                this.appendOutput('        inet6 ::1  prefixlen 128  scopeid 0x10<host>\n');
            }

            downloadFile(url) {
                if (!url) {
                    this.appendOutput('<span class="error">wget: missing URL</span>\n');
                    return;
                }

                this.appendOutput(`--${new Date().toISOString()}--  ${url}\n`);
                this.appendOutput('Resolving hostname... 192.168.1.200\n');
                this.appendOutput('Connecting to hostname|192.168.1.200|:80... connected.\n');
                this.appendOutput('HTTP request sent, awaiting response... 200 OK\n');
                this.appendOutput('Length: 1024 (1.0K) [text/html]\n');
                this.appendOutput('Saving to: \'index.html\'\n\n');
                this.appendOutput('index.html      100%[===================>]   1.00K  --.-KB/s    in 0s\n\n');
                this.appendOutput(`${new Date().toISOString()} (--.-KB/s) - 'index.html' saved [1024/1024]\n`);
                
                // Add downloaded file to current directory
                const currentDir = this.getCurrentDirectory();
                if (currentDir) {
                    const filename = url.split('/').pop() || 'index.html';
                    currentDir.children[filename] = { 
                        type: 'file', 
                        content: `Downloaded content from ${url}` 
                    };
                }
            }

            changePermissions(mode, file) {
                if (!mode || !file) {
                    this.appendOutput('<span class="error">chmod: missing operand</span>\n');
                    return;
                }

                const currentDir = this.getCurrentDirectory();
                if (currentDir && currentDir.children && currentDir.children[file]) {
                    this.appendOutput(`<span class="success">Changed permissions of '${file}' to ${mode}</span>\n`);
                    if (mode.includes('x') || mode === '755' || mode === '777') {
                        currentDir.children[file].executable = true;
                    }
                } else {
                    this.appendOutput(`<span class="error">chmod: cannot access '${file}': No such file or directory</span>\n`);
                }
            }

            changeOwner(owner, file) {
                if (!owner || !file) {
                    this.appendOutput('<span class="error">chown: missing operand</span>\n');
                    return;
                }

                const currentDir = this.getCurrentDirectory();
                if (currentDir && currentDir.children && currentDir.children[file]) {
                    this.appendOutput(`<span class="success">Changed owner of '${file}' to ${owner}</span>\n`);
                } else {
                    this.appendOutput(`<span class="error">chown: cannot access '${file}': No such file or directory</span>\n`);
                }
            }

            changePassword(user) {
                const targetUser = user || 'user';
                this.appendOutput(`Changing password for ${targetUser}.\n`);
                this.appendOutput('(current) UNIX password: ');
                this.appendOutput('<span class="success">Password changed successfully</span>\n');
            }

            tarOperation(args) {
                if (args.length < 2) {
                    this.appendOutput('<span class="error">tar: missing operand</span>\n');
                    return;
                }

                const options = args[1];
                if (options.includes('c')) {
                    const archive = args[2];
                    const files = args.slice(3);
                    this.appendOutput(`<span class="success">Created archive '${archive}' with ${files.length} files</span>\n`);
                } else if (options.includes('x')) {
                    const archive = args[2];
                    this.appendOutput(`<span class="success">Extracted archive '${archive}'</span>\n`);
                } else if (options.includes('t')) {
                    const archive = args[2];
                    this.appendOutput(`Contents of '${archive}':\n`);
                    this.appendOutput('file1.txt\nfile2.txt\ndirectory/\n');
                }
            }

            zipFiles(args) {
                if (args.length < 3) {
                    this.appendOutput('<span class="error">zip: missing operand</span>\n');
                    return;
                }

                const archive = args[1];
                const files = args.slice(2);
                this.appendOutput(`  adding: ${files.join(', ')}\n`);
                this.appendOutput(`<span class="success">Created archive '${archive}'</span>\n`);
            }

            unzipFile(archive) {
                if (!archive) {
                    this.appendOutput('<span class="error">unzip: missing operand</span>\n');
                    return;
                }

                this.appendOutput(`Archive:  ${archive}\n`);
                this.appendOutput('  inflating: file1.txt\n');
                this.appendOutput('  inflating: file2.txt\n');
                this.appendOutput(`<span class="success">Extracted '${archive}'</span>\n`);
            }

            sshConnect(target) {
                if (!target) {
                    this.appendOutput('<span class="error">ssh: missing hostname</span>\n');
                    return;
                }

                this.appendOutput(`Connecting to ${target}...\n`);
                this.appendOutput('The authenticity of host cannot be established.\n');
                this.appendOutput('ECDSA key fingerprint is SHA256:abc123def456.\n');
                this.appendOutput('<span class="success">Connection established</span>\n');
                this.appendOutput(`<span class="warning">This is a simulation - no actual SSH connection made</span>\n`);
            }

            scpTransfer(src, dst) {
                if (!src || !dst) {
                    this.appendOutput('<span class="error">scp: missing operand</span>\n');
                    return;
                }

                this.appendOutput(`${src}                    100% 1024     1.0MB/s   00:00\n`);
                this.appendOutput(`<span class="success">File transferred successfully</span>\n`);
            }

            clearScreen() {
                document.getElementById('output').innerHTML = '';
            }

            getCurrentDirectory() {
                return this.getDirectoryByPath(this.currentPath);
            }

            getDirectoryByPath(path) {
                const parts = path.split('/').filter(p => p);
                let current = this.fileSystem['/'];

                for (const part of parts) {
                    if (current && current.children && current.children[part]) {
                        current = current.children[part];
                    } else {
                        return null;
                    }
                }

                return current;
            }

            updatePrompt() {
                const prompt = document.getElementById('prompt');
                const displayPath = this.currentPath === '/home/user' ? '~' : this.currentPath;
                prompt.textContent = `user@linux:${displayPath}$`;
            }

            appendOutput(text) {
                const output = document.getElementById('output');
                output.innerHTML += text;
                output.scrollTop = output.scrollHeight;
            }
        }

        // Initialize the simulator
        const simulator = new LinuxSimulator();
    </script>
</body>
</html>